﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SCCL2010
{
    public static class Lists
    {
        /// <summary>
        /// returns whether or not a SharePoint Web contains a list.
        /// </summary>
        /// <param name="oWeb"></param>
        /// <param name="cList"></param>
        /// <returns></returns>
        public static bool HasList(SPWeb oWeb, String cList)
        {
            try
            {
                foreach (SPList list in oWeb.Lists)
                {
                    if (list.Title == cList)
                        return true;
                }
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(oWeb, "ListFunctions.HasList", ex);
            }
            return false;
        }

        /// <summary>
        /// Hides a SharePoint List.
        /// </summary>
        /// <param name="oWeb"></param>
        /// <param name="cListName"></param>
        public static void HideList(SPWeb oWeb, String cListName)
        {
            try
            {
                SPList oList = oWeb.Lists[cListName];
                oList.Hidden = true;
                oList.Update();
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(oWeb, "Lists.HideList", ex);
            }
        }

        /// <summary>
        /// Un Hides a SharePoint List.
        /// </summary>
        /// <param name="oWeb"></param>
        /// <param name="cListName"></param>
        public static void UnHideList(SPWeb oWeb, String cListName)
        {
            try
            {
                SPList oList = oWeb.Lists[cListName];
                oList.Hidden = false;
                oList.Update();
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(oWeb, "ListFunctions.UnHideList", ex);
            }
        }

        /// <summary>
        /// Checks if the SPRoleDefinition for the Specified List equals "read"
        /// </summary>
        /// <param name="lst"></param>
        /// <returns>returns boolean value whether the SPRoleDefinition equals "read"</returns>
        public static bool IsReadOnly(SPList lst)
        {
            foreach (SPRoleDefinition def in lst.AllRolesForCurrentUser)
            {
                if (def.Name.ToLower() == "read")
                    return true;
            }
            return false;
        }

        public static bool HasView(SPList oList, string cViewName)
        {
            try
            {
                foreach (SPView view in oList.Views)
                {
                    if (view.Title == cViewName)
                        return true;
                }
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError("HasView - " + cViewName, ex);
            }
            return false;
        }

        /// <summary>
        /// Gets the default view of the specified List.
        /// </summary>
        /// <param name="oList"></param>
        /// <returns>returns a SPView object representing the view of the specified List.</returns>
        public static SPView GetDefaultView(SPList oList)
        {
            foreach (SPView view in oList.Views)
            {
                if (view.DefaultView)
                    return view;
            }
            return null;
        }


    }
}
